package models;

import play.mvc.*;
import play.libs.*;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import akka.actor.*;
import static akka.pattern.Patterns.ask;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.*;

import models.msgs.*;
import models.msgs.Number;
import static java.util.concurrent.TimeUnit.*;

public class Table extends UntypedActor {

	@SuppressWarnings("unused")
	private Socket[] clientsocket;
	
	private int smallblind;
	private int howgoldgold;
	private int bigblind;
	private int playersnumber;
	private int walleta;
	private int firstplayer;
	private Integer whatround;
	private Deck deck;
	private boolean whatroundlong;
	
	private int maxgoldgold;
	 BufferedReader[] in = null;
	  PrintWriter[] out = null;
	  Random randomizator;
   private String tabelcondition[];
   private int tabelgolden[];
   private int tabelgoldingame[];
   private int tabelorder[];
   @SuppressWarnings("unused")
private String[][] card;
	@SuppressWarnings("unused")
	private int pnu;
	
    // Default table.
    static ActorRef defaultTable = Akka.system().actorOf(Props.create(Table.class));

    // Members of this table.
    static Map<String, ActorRef> members = new HashMap<String, ActorRef>();

    public static void join(final String name, WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out) throws Exception {
        
    	
    	
    	
    	
        // Send the Join message to the table
        String result = (String) Await.result(ask(defaultTable, new Join(name), 1000), Duration.create(1, SECONDS));

        if ("OK".equals(result)) {

            ActorRef player = Akka.system().actorOf(
                    Props.create(Human.class, name, in, out, defaultTable));

            members.put(name, player);
            String text = "my Actor has been added to Table";
            notifyAll(new Info(text, name));
        }
    }

    public static void quit(final String username) throws Exception {

        //to do
    }


    
     public void Clientisking() 
 	{
 	playersnumber=2;
 	this.walleta=25;
    this.smallblind=1;
 	this.bigblind=2;
 	 randomizator = new Random();
 	tabelcondition= new String[playersnumber+1];
    tabelcondition[0]="empty";
    tabelgolden= new int[playersnumber+1];
    tabelgolden[0]=0;
    tabelgoldingame= new int[playersnumber+1];
    tabelgoldingame[0]=0;
    tabelorder= new int[playersnumber+1];
    tabelorder[0]=0;
    card=new String[playersnumber+1][4];
    card[0][0]="empty";
    card[0][1]="empty";
    card[0][2]="empty";
    card[0][3]="empty";
    maxgoldgold=0;
    	try
    	{   in= new BufferedReader[playersnumber];
    	    out=new PrintWriter[playersnumber];
    		for(int i=0; i<playersnumber; i++)
    		{
    		in[i] = new BufferedReader(new InputStreamReader(clientsocket[i].getInputStream()));
    		out[i] = new PrintWriter(clientsocket[i].getOutputStream(), true);
    		clientsocket[i].setSoTimeout(360000);
    		}
    	  
    	 
    		game();
    		int gaminf;
    		gaminf=playersnumber;
    		
    		for(int i=1; i<=playersnumber; i++)		
    		{
    			if(tabelcondition[i].equals("Fold")){gaminf--;}
    		}
    		
    		
    		Thread.sleep(20000);
    		
    		
    		
    		while(true)
    			
    		{   firstplayer=twoplayer(firstplayer);
    			game2(firstplayer);
    			 System.out.println(" " + gaminf);
    			gaminf=playersnumber;
    			for(int i=1; i<=playersnumber; i++)		
    			{ 
    			if(tabelcondition[i].equals("Fold")){gaminf--;}
    			}
    		    if(gaminf<2) break;
    		    
	    
	    		Thread.sleep(20000);
	    	
    		  System.out.println(" 44" + gaminf);
    		}
    		
    		for(int i=0; i<playersnumber; i++)		
    				    		{	out[i].println("end");	
    					    		in[i].close();		
    					    		out[i].close();		
    						
    			    		    }
    	}
    	catch (IOException e)
    	{
        System.out.println(" Problem : I/O ");
    	} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	


    	

	
	}
    
    
    
    @SuppressWarnings("unused")
	public void onReceive(Object message) throws Exception {

        if (message instanceof Join) {

            Join join = (Join) message;

            if (false) {
                // if username is already taken do sth
            } else {
                getSender().tell("OK", getSelf());
            }
        } else if (message instanceof Number) {
            int nr = ((Number) message).getNumber();
            String name = ((Number) message).getName();
            getSender().tell(Deck.giveCard(nr), getSelf());
            String text = "I've sent    " ;
            notifyAll(new Info(text, "Table"));
        } else if (message instanceof Info) {
            String text = ((Info) message).getText();
            String name = ((Info) message).getName();

            notifyAll(new Info(text, name));
        } else if (message instanceof Quit) {

            String name = ((Quit) message).getName();
            members.remove(name);
            String text = "has left the game";
            notifyAll(new Info(text, name));


        } else {
            unhandled(message);
        }

    }


    static public void notifyAll(Object msg) {
        for (ActorRef member : members.values()) {
            member.tell(msg, defaultTable);
        }
    }
    
    
	 public void Order(int ffirtstplayer)
	 { int endplayer;
		 endplayer=0;
		 tabelorder[0]=0;
		 for(int b=ffirtstplayer; b<=playersnumber; b++ )
			{ if(!tabelcondition[b].equals("Fold") || !tabelcondition[b].equals("Allin") || !tabelcondition[b].equals("niekatywny "))
				tabelorder[b]=++endplayer;
			else tabelorder[b]=0;
			}
			
			for(int h=1; h<ffirtstplayer; h++ )
			{    if(!tabelcondition[h].equals("Fold") || !tabelcondition[h].equals("Allin") || !tabelcondition[h].equals("niekatywny "))
				tabelorder[h]=++endplayer;
			else tabelorder[h]=0;
			}
	 }
	 
	 public int twoplayer(int ffirstplayer)
	 {if(ffirstplayer==playersnumber)
	 		{
		     return 1;
	 		}
	 else return ++ffirstplayer;
		 
	 }
	 
	 public int threeplayer(int ffirstplayer)
	 {if(ffirstplayer==playersnumber-1)
	 		{
		     return 1;
	 		}
	 else if(ffirstplayer==playersnumber)
		{
	     return 2;
		}
	 else
		 return ffirstplayer=ffirstplayer+2;
		 
	 }
	 
	 public int fourplayer(int ffirstplayer)
	 {if(ffirstplayer==playersnumber-2)
	 		{
		     return 1;
	 		}
	 else if(ffirstplayer==playersnumber-1)
		{
	     return 2;
		}
	 else if(ffirstplayer==playersnumber)
		{
	     return 3;
		} else
		 
		 
		 return ffirstplayer=ffirstplayer+3;
		 
	 }
	
	 
	 
	 
	 public void game() 
	 { deck= new Deck(playersnumber);
		 int t;
		 
		 whatround=1;
		 String cos;
		
		 firstplayer=1+randomizator.nextInt(playersnumber);
		 for(int i=0; i<playersnumber; i++)
		 {
			 t=i+1;
			 
			  
			    tabelcondition[t]="aktywny";
			   if( twoplayer(firstplayer)!=t   && threeplayer(firstplayer)!=t)
			   { tabelgolden[t]=walleta;
			    tabelgoldingame[t]=0;
			   }
			    else if( twoplayer(firstplayer)==t)
			    {   tabelcondition[t]="Ma�� ciemn� da�";
			    	tabelgolden[t]=walleta-smallblind;
				    tabelgoldingame[t]=smallblind;
			    }
			    else {  tabelcondition[t]="Du�� ciemn� da�";
			    	tabelgolden[t]=walleta-bigblind;
			    tabelgoldingame[t]=bigblind;
			    	
			    	 }
			    tabelorder[t]=0; 
			    deck.plushand(t);
			    deck.plushand(t);
			    deck.plushand(t);
			    deck.plushand(t);
			    maxgoldgold=bigblind;
			   cos= "infostart " +playersnumber + " " + walleta +" " +  twoplayer(firstplayer) + " " + threeplayer(firstplayer) + " " + t + " "+ deck.showcard(t,0) + " "+  deck.showcard(t,1) + " " + deck.showcard(t,2) +  " " +  deck.showcard(t,3) + " " + fourplayer(firstplayer) + " " + smallblind + " " + bigblind;
			out[i].println(cos );
			System.out.println("Informacje podanne graczowi:" + t );
			
		 }
		 
		 Order(fourplayer(firstplayer));
		 //int endplayer;
		// endplayer=0;
		/// Format data from server; " Auction yourauction playnumer welcherunde" || " Auction info playnumer  condition numberoftokens rate "
		 auctionwhile(fourplayer(firstplayer), true);
		for(int k=1;k<=3;k++)
			{
				cardfor();
			 		++whatround;
					Ordersend();
	
					auctionwhile(twoplayer(firstplayer), false);
			}
		 int gaminf=playersnumber;
		for(int i=1; i<=playersnumber; i++)		
		{   gaminf=playersnumber;
		if(tabelcondition[i].equals("Fold")){gaminf--;}
		}
	    if(gaminf>=2) 
		 winnerisis(playersnumber);
	    else for(int i=0; i<playersnumber; i++)		
		{	out[i].println("end");	}
		 				///Sprawdzi� czy wszyscy maj� po tyle samo stawki z wyj�tkiem all in i fold
						
		 
	 }
	 
	 
	 
	 public void game2(int fplayer) 
	 { deck= new Deck(playersnumber);
		 int t;
		 
		 whatround=1;
		 String cos;
		
		
		 for(int i=0; i<playersnumber; i++)
		 {
			 t=i+1;
			 
			  
			    tabelcondition[t]="aktywny";
			   if( twoplayer(firstplayer)!=t   && threeplayer(firstplayer)!=t)
			   { tabelgolden[t]=tabelgolden[t];
			    tabelgoldingame[t]=0;
			   }
			    else if( twoplayer(firstplayer)==t)
			    {   tabelcondition[t]="Ma�� ciemn� da�";
			    	tabelgolden[t]=tabelgolden[t]-smallblind;
				    tabelgoldingame[t]=smallblind;
			    }
			    else {  tabelcondition[t]="Du�� ciemn� da�";
			    	tabelgolden[t]=tabelgolden[t]-bigblind;
			    tabelgoldingame[t]=bigblind;
			    	
			    	 }
			    tabelorder[t]=0; 
			    deck.plushand(t);
			    deck.plushand(t);
			    deck.plushand(t);
			    deck.plushand(t);
			    maxgoldgold=bigblind;
			   cos= "infostart2 " +playersnumber + " " + walleta +" " +  twoplayer(fplayer) + " " + threeplayer(fplayer) + " " + t + " "+ deck.showcard(t,0) + " "+  deck.showcard(t,1) + " " + deck.showcard(t,2) +  " " +  deck.showcard(t,3) + " " + fourplayer(fplayer) + " " + smallblind + " " + bigblind;
			out[i].println(cos );
			System.out.println("Informacje podanne graczowi:" + t );
			
		 }
		 
		 Order(fourplayer(fplayer));
		 //int endplayer;
		// endplayer=0;
		/// Format data from server; " Auction yourauction playnumer welcherunde" || " Auction info playnumer  condition numberoftokens rate "
		 auctionwhile(fourplayer(fplayer), true);
		for(int k=1;k<=3;k++)
			{
			 	cardfor();
			 		++whatround;
					Ordersend(fplayer);
	
						auctionwhile(twoplayer(fplayer), false);
						 int gaminf=playersnumber;
							for(int i=1; i<=playersnumber; i++)		
							{   gaminf=playersnumber;
							if(tabelcondition[i].equals("Fold")){gaminf--;}
							}
						    if(gaminf>=2) break;			
			}
		 int gaminf=playersnumber;
			for(int i=1; i<=playersnumber; i++)		
			{   gaminf=playersnumber;
			if(tabelcondition[i].equals("Fold")){gaminf--;}
			}
		    if(gaminf>=2) 
			 winnerisis(playersnumber);
		    else for(int i=0; i<playersnumber; i++)		
			{	out[i].println("end");	}
		 				///Sprawdzi� czy wszyscy maj� po tyle samo stawki z wyj�tkiem all in i fold
						
		 
	 }
	 
	 
	public Boolean   ifcan(String whatdo, Integer i, Integer howgold)
	{ 
		if( whatdo.equals("Check") )
			{ String oldaction;
			 
		     if (tabelorder[i]==1 &&  tabelgoldingame[i]==maxgoldgold) whatroundlong=false;
		     else  whatroundlong=true;
			 if(tabelorder[i]!=1) {if(i-1>0) oldaction=tabelcondition[i-1]; else oldaction=tabelcondition[playersnumber]; }else oldaction="empty";
			 int k; if(i-1>0){ k=i-1;} else{ k=playersnumber;}
		     while((oldaction.equals("Fold") || oldaction.equals("Allin")) && k!=i) {   if(tabelorder[i]!=1) {if(k-1>0) oldaction=tabelcondition[k-1]; else oldaction=tabelcondition[playersnumber]; }else oldaction="empty";  if(i-1>0){ k=k-1;} else{ k=playersnumber;}}
			 return Auctionaction.Bcheckaction(  tabelgoldingame[i],  maxgoldgold,  tabelorder[i], whatroundlong,oldaction,  tabelcondition[i]);
			}
		else if( whatdo.equals("Bet") )
		{ String oldaction;
		if(tabelorder[i]!=1) {if(i-1>0) oldaction=tabelcondition[i-1]; else oldaction=tabelcondition[playersnumber]; }else oldaction="empty";
		 int k; if(i-1>0){ k=i-1;} else{ k=i;}
	     while((oldaction.equals("Fold") || oldaction.equals("Allin")) && k!=i) {   if(i-1>0){ k=i-1;} else{ k=i;}if(tabelorder[i]!=1) {if(k-1>0) oldaction=tabelcondition[k-1]; else oldaction=tabelcondition[playersnumber]; }else oldaction="empty";}
		 return Auctionaction.Bbetaction(  tabelorder[i],  whatroundlong, oldaction, maxgoldgold,  tabelgoldingame[i],  howgoldgold, tabelgolden[i]);
		}
		else if( whatdo.equals("Raise") )
		{   // System.out.print(  "Call "+ tabelcondition[i] + " " +  tabelgolden[i] + " " + tabelgoldingame[i]+  " " + tabelorder[i] + " " + maxgoldgold +  " " + howgoldgold);
		 return Auctionaction.Braiseaction(  maxgoldgold, tabelgoldingame[i],  howgoldgold,tabelgolden[i]);
		}
		else if( whatdo.equals("Call") )
		{    
           System.out.print(  "Call "+ tabelcondition[i] + " " +  tabelgolden[i] + " " + tabelgoldingame[i]+  " " + tabelorder[i] + " " + maxgoldgold +  " " + howgoldgold);
		
		 
		
		 return Auctionaction.Bcallaction( maxgoldgold, tabelgoldingame[i],tabelgolden[i]);
		}
		
		else if( whatdo.equals("Fold") )
		{ 
		 return Auctionaction.Bfoldaction();
		}
		else if( whatdo.equals("Allin") )
		{ 
		 return Auctionaction.Ballin( tabelgolden[i]);
		}
		else return false;
		
		
		
		
	
	}
	
	
	public void auctionT(Integer h)
	{   	if( !tabelcondition[h].equals("Fold")  && !tabelcondition[h].equals("preFold")   && !tabelcondition[h].equals("Allin") &&  !tabelcondition[h].equals("nieaktywny"))
		{
		    try{	 String cos2;
					String sendinfoplayer;
					cos2= "auction " +"yourauction" + " " + h +" " + whatround ;
			
					out[h-1].println(cos2);
			
					String whatdo2 = "";
			
					System.out.println(cos2);
					whatdo2=in[h-1].readLine();
					String[] whatdo=whatdo2.split(" ");
					System.out.println(" ttt" + h + whatdo2 );
					if(whatdo.length==3 || whatdo.length==2 )	
					{ 
				
						if(  whatdo[0].equals("auctionmydecision"))
						{	try{  if( whatdo.length==3) howgoldgold=Integer.parseInt(whatdo[2]); else howgoldgold=0;}
							catch (NumberFormatException e) 
							{} 
								if(  ifcan(whatdo[1],h,howgoldgold) )
									{  String selectaction=whatdo[1];
										
										if(selectaction.equals("Bet"))
											{  tabelgolden[h]=tabelgolden[h]-howgoldgold; tabelgoldingame[h]=tabelgoldingame[h]+howgoldgold;  maxgoldgold=tabelgoldingame[h];   }
											else if(selectaction.equals("Raise"))
											{  tabelgolden[h]=tabelgolden[h]-howgoldgold; tabelgoldingame[h]=tabelgoldingame[h]+howgoldgold;  maxgoldgold=tabelgoldingame[h];       }
							
											else if(selectaction.equals("Call"))
											{ tabelgolden[h]= tabelgolden[h]-(maxgoldgold-tabelgoldingame[h]); tabelgoldingame[h]=maxgoldgold;      }
											else if(selectaction.equals("Allin"))
											{  tabelgoldingame[h]= tabelgoldingame[h]+tabelgolden[h]; tabelgolden[h]=0;      }
								
										tabelcondition[h]=selectaction;
						       
										sendinfoplayer="auction info ";
										sendinfoplayer=sendinfoplayer  + h + " " +  tabelcondition[h] + " " +  tabelgolden[h]  + " " + tabelgoldingame[h] ;
										
										for(int i=0; i<playersnumber; i++)
										{   out[i].println(sendinfoplayer );
										System.out.println("Informacje podanne graczom o graczu...:" + h  + sendinfoplayer);
							 			
										
										}
								
								
										
									}
								
								 else 
								 	{     tabelcondition[h]="Fold";
								       
												sendinfoplayer="auction info ";
												sendinfoplayer=sendinfoplayer  + h + " " +  tabelcondition[h] + " " +  tabelgolden[h]  + " " + tabelgoldingame[h] ;
										
												for(int i=0; i<playersnumber; i++)
												{   out[i].println(sendinfoplayer );
												System.out.println("Informacje podanne graczom o graczu...:" + h  + sendinfoplayer);
								 			
												}
												
												System.out.println("Pr�bowa�on oszukiwa�:" + h);
									 
								 	}
						}
					}
			
	      
           
			
			}
  			catch (IOException e)
				{
				System.out.println(" Problem z clientem");
				tabelcondition[h]="Fold";
	               
				String sendinfoplayer2="auction info ";
				sendinfoplayer2=sendinfoplayer2  + h + " " +  tabelcondition[h] + " " +  tabelgolden[h]  + " " + tabelgoldingame[h] ;
		
				for(int i=0; i<playersnumber; i++)
				{   out[i].println(sendinfoplayer2 );
				System.out.println("Informacje podanne graczom o graczu...:" + h  + sendinfoplayer2);
			
				}
				
				System.out.println("Od��czy� si�:" + h);
				
				}
		}  else {    
			if( tabelcondition[h].equals("preFold") )
			
				{ tabelcondition[h]="Fold";
	               
					String sendinfoplayer2="auction info ";
					sendinfoplayer2=sendinfoplayer2  + h + " " +  tabelcondition[h] + " " +  tabelgolden[h]  + " " + tabelgoldingame[h] ;
			
					for(int i=0; i<playersnumber; i++)
					{   out[i].println(sendinfoplayer2 );
					System.out.println("Informacje podanne graczom o graczu...:" + h  + sendinfoplayer2);
	 			
					}
					
					System.out.println("Od��czy� si�:" + h);
				}
		 
	 	}
		
	}
	
	
	public void Ordersend()
	{
		String sendinfoplayer2="order ";
		sendinfoplayer2=sendinfoplayer2 +  twoplayer(firstplayer) + " " + whatround;

		for(int i=0; i<playersnumber; i++)
		{   out[i].println(sendinfoplayer2 );
		
		
		}
		Order(twoplayer(firstplayer));
		System.out.println("Informacje podanne graczom o kolejno�ci"   + sendinfoplayer2);
	}
	
	
	public void Ordersend4(int fplayer)
	{
		String sendinfoplayer2="order ";
		sendinfoplayer2=sendinfoplayer2 +  fplayer + " " + whatround;

		for(int i=0; i<playersnumber; i++)
		{   out[i].println(sendinfoplayer2 );
		
		
		}
		Order(fplayer);
		System.out.println("Informacje podanne graczom o kolejno�ci"   + sendinfoplayer2);
	}
	public void Ordersend(int fplayer)
	{
		String sendinfoplayer2="order ";
		sendinfoplayer2=sendinfoplayer2 +  twoplayer(fplayer) + " " + whatround;

		for(int i=0; i<playersnumber; i++)
		{   out[i].println(sendinfoplayer2 );
		
		
		}
		Order(twoplayer(fplayer));
		System.out.println("Informacje podanne graczom o kolejno�ci"   + sendinfoplayer2);
	}
	
	
	
	public void auctionwhile(Integer ffplayer,Boolean wwatroundlong)
	{///Pierwsza rudna czwarty gracz, reszta drugi gracz.
		
		whatroundlong=wwatroundlong;
	
      				
      for(int b=ffplayer; b<=playersnumber; b++ )
				{	auctionT(b);
				}
			for(int h=1; h<ffplayer; h++ )
				{     
				 auctionT(h);
				
				}
			
		
		whatroundlong=true;
		 for(int b=ffplayer; b<=playersnumber; b++ )
			{	if(ifround()==false) auctionT(b);
			}
		for(int h=1; h<ffplayer; h++ )
			{   if(ifround()==false)   
			       auctionT(h);
			
			}
		
	}
	
	public Boolean ifround()
	{ 	Boolean allr;
	 int max;
		
		allr=true;
	 max=tabelgoldingame[1];
	 	for(int i=2; i<=playersnumber; i++)
	 	{   if(!tabelcondition[i].equals("Fold")  &&  !tabelcondition[i].equals("Allin")  &&  !tabelcondition[i].equals("nieaktywny") )
	 		{  if( !tabelcondition[i].equals("Allin") )
		               {if(max!=tabelgoldingame[i])
		           					allr=false;
		                  System.out.println(" " +  max + " " + tabelgoldingame[i] ); 
		           			}
		         else {if(max<tabelgoldingame[i])
		        	   System.out.println(" " +  max + " " + tabelgoldingame[i] ); 
    					allr=false;
    			       }
	 		} 
	 	}
	 	 
	 	return allr;
		
		
	}
	
	public void cardfor()
	{///Pierwsza rudna czwarty gracz, reszta drugi gracz.
		
		
			for(int b=1; b<=playersnumber; b++ )
				{ 	ccardchange(b);
				}
			
				
				
		
		
	}
	
	
	public void winnerisis(int pplayersnumber)
	  
	{  Powercards pc= new Powercards( deck);
		
		int [] win=null;
		
	    win=pc.Winneris( pplayersnumber, tabelcondition);
		int le=win.length;
		for(int b=1; b<=pplayersnumber; b++ )
		{System.out.println("gracz tak� r�ke mia� i tyle punkt�w z niej zyska�:" + b + ":: " + deck.showcard(b, 0) + " " + deck.showcard(b, 1) + " " +deck.showcard(b, 2) + " " +deck.showcard(b, 3) + " " +pc.powerhand( deck.showcard(b, 0),deck.showcard(b, 1),deck.showcard(b, 2),deck.showcard(b, 3)));
		} 
				
		
		String infowin="win info";
		for(int b=0; b<le; b++ )
		{  if(!tabelcondition.equals("Fold") )
			infowin=infowin+ " " + win[b];
		}
		for(int i=0; i<playersnumber; i++)
		{   out[i].println(infowin );
		
		
		}System.out.println("Informacje podanne o wygranej" + infowin);
		if(le==1)
				{  if(!tabelcondition.equals("Allin") )
			     tabelgolden[win[0]]=tabelgolden[win[0]]+maxgoldgold*playersnumber;
				   else{ for(int b=1; b<=pplayersnumber; b++ )
						{if(win[0]!=b)
					     {  if(tabelgoldingame[b]>=tabelgoldingame[0])
							{tabelgolden[win[0]]=tabelgolden[win[0]]+tabelgoldingame[0];
							 tabelgolden[b]= tabelgolden[b]+tabelgoldingame[b]-tabelgoldingame[0];
							
							}else {tabelgolden[win[0]]=tabelgolden[win[0]]+tabelgoldingame[b];
							      
							      }
					     
					     }
					   
						}
				   }
				}
		
		
		
	}
	
	
	public void ccardchange(Integer h)
	{
		if( !tabelcondition[h].equals("Fold")   &&   !tabelcondition[h].equals("preFold")   &&  !tabelcondition[h].equals("nieaktywny"))
		{// co si� dzieje z wymina gdy prefold?
		    try{	 String cos2;
					//String sendinfoplayer;
					cos2= "changehand " +"yourchangecard "  + h +" " + whatround ;
			
					out[h-1].println(cos2);
			
					String whatdo2 = "";
			
					System.out.println(cos2);
					whatdo2=in[h-1].readLine();
					String[] whatdo=whatdo2.split(" ");
					System.out.println(" Gracz otrzyma� informacje:" + h + whatdo2 );
					if(whatdo.length<=6 && whatdo.length>=2 )	
					{  if(  whatdo[0].equals("deleteandgivenewcards"))
						{  if(  !whatdo[1].equals("empty"))
							{    	try{ int numcard=Integer.parseInt( whatdo[1] );
							            if(numcard>=1 && numcard<=4)
							            	{ 
							            	  for(int i=2; i<2+numcard; i++)
							            		{ deck.minushand(h,whatdo[i]);
							            		   deck.plushand(h);
							            		
							            		}
							            	   System.out.println("Zmieniono karty" + h );
							            	}
							            out[h-1].println("newcard " + h + " " + deck.showcard(h, 0) + " " + deck.showcard(h, 1) + " " + deck.showcard(h,2) + " " + deck.showcard(h, 3));
							            System.out.println("Karty podanne graczu:" + h  + numcard);
							            for(int i=0; i<playersnumber; i++)
										{   out[i].println("changehand info "  + h + " " +numcard);
										
						 			
										}	System.out.println("Informacje podanne graczom o graczu...:" + h  + numcard);
							           }
									catch (NumberFormatException e) 
									{} 
									catch (NullPointerException e) 
									{} 
							        
							} else {for(int i=0; i<playersnumber; i++)
										{   out[i].println("changehand info "  + h + " " +"�adnych");
											System.out.println("Informacje podanne graczom o graczu...:" + h  + " empty");
			 			
										}
								
									}
						
						}
						
						
					}
					
		    }catch (IOException e)
			{tabelcondition[h]="preFold";
			System.out.println(" Problem z clientem");
			}
		}
	 
	}
    
    


}

